package com.class3.team.fossilProject.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.class3.team.fossilProject.service.BookService;

@CrossOrigin
@RestController
@RequestMapping("/homepage")
public class HomePageController {
	
	@Autowired
	private BookService bookS; 
	
	// 得到随机十个点位，按照对应的格式返回
	@GetMapping("/location")
	@ResponseBody
//	public JSONArray get() {
//		JSONArray res = new JSONArray();
//		Integer [] intArray = new Integer[50];
//		for(int i = 1; i < 51;i++) {
//			intArray[i-1] = i;
//		}
//		for(int i = 0; i < intArray.length; i ++) {
//			JSONObject temp = bookS.getBookById(intArray[i]);
//			res.add(temp);
//		}
//		return res;
//	}

		public JSONArray get() {
		JSONArray res = new JSONArray();
		Integer [] intArray = new Integer[3174];
		for(int i = 1; i < 3175;i++) {
			intArray[i-1] = i;
		}
		for(int i = 0; i < intArray.length; i ++) {
			JSONObject temp = bookS.getBookById(intArray[i]);
			JSONObject temp1 = new JSONObject();
			temp1 = (JSONObject) temp.get("geometry");
			JSONArray temp2 = new JSONArray();
			temp2 = (JSONArray) temp1.getJSONArray("coordinates");
			JSONObject fin = new JSONObject();
			fin.put("lat", temp2.get(1));
			fin.put("lng", temp2.get(0));
			res.add(fin);
		}
		return res;
	}
}
